package com.graduation.fq.teacher.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.graduation.fq.teacher.R;
import com.graduation.fq.teacher.interfaces.EventListener;
import com.graduation.fq.teacher.logic.LoginRegisterLogic;
import com.graduation.fq.teacher.module.EventArgs;
import com.graduation.fq.teacher.module.SimpleEventArgs;
import com.graduation.fq.teacher.utils.CommonUtils;
import com.graduation.fq.teacher.utils.Const;
import com.graduation.fq.teacher.utils.ToastUtils;

public class RegisterActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        findViewById(R.id.register_register_bt).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String name = ((EditText) (findViewById(R.id.register_teacher_name))).getText().toString();
                String password = ((EditText) (findViewById(R.id.register_teacher_pwd))).getText().toString();
                register(name, password);
            }
        });
    }

    private void register(String name, String password) {
        if (CommonUtils.isNotNullOrEmpty(name, password)) {
            LoginRegisterLogic.getInstance().register(name, password, new EventListener() {

                @Override
                public void onEvent(EventArgs args) {
                    SimpleEventArgs<Boolean> arg = (SimpleEventArgs<Boolean>) args;
                    if (arg.getStatus() != Const.HTTP_SUCCESS) {
                        ToastUtils.Toast(RegisterActivity.this, "注册失败, 请重新注册");
                    } else if (!arg.getValue()) {
                        ToastUtils.Toast(RegisterActivity.this, "该用户名已被注册, 请尝试其他用户名!");
                    } else {
                        startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        finish();
                    }
                }
            });
        }
    }
}
